<?php

class Order extends Model
{
    # Lấy tất cả danh sách đơn hàng
    /*
     * filter: not_confirmed : chua xac nhan
     * filter: not_delivered : chua giao hang
     *
     * counter = true nếu muốn đếm số dòng dữ liệu
     */

    const FILTER_ALL = 0;
    const FILTER_NOT_CONFIRMED = 1; # 0001
    const FILTER_NOT_DELIVERED = 2; # 0010
    const FILTER_BY_USER_ID = 4;    # 0100
    const FILTER_ODER_COUNTER = 8; # 1000
    public static function getOrders($filter = self::FILTER_ALL, $user_id = 1)
    {
        $where = '(1=1)';
        $bind = array();
        if ($filter & self::FILTER_NOT_CONFIRMED) {
            $where .= ' AND ddh_trangthaixn = :confirmed';
            $bind['confirmed'] = 0;
        }
        if ($filter & self::FILTER_NOT_DELIVERED) {
            $where .= ' AND ddh_trangthaigh = :delivered';
            $bind['delivered'] = 0;
        }
        if ($filter & self::FILTER_BY_USER_ID) {
            $where .= ' AND u.u_id = :uid';
            $bind['uid'] = $user_id;
        }

        $sql = <<<SQL
SELECT d.ddh_id, DATE_FORMAT(ddh_ngaydh, '%d/%m/%Y') AS ddh_ngaydh, u_ten, u.u_id,
       ddh_trangthaixn, ddh_trangthaigh,
       SUM(ctddh_soluongdh * ctddh_dongiadh) AS total
FROM dondathang d NATURAL JOIN mbuser u NATURAL JOIN chitietddh c
GROUP BY d.ddh_id
HAVING {$where}
ORDER BY d.ddh_ngaydh DESC
SQL;
        $data = self::$_database->run($sql, $bind);
        if ($filter & self::FILTER_ODER_COUNTER) {
            return count($data);
        } else {
            foreach ($data as &$row) {
                $row['total'] = App::formatVnd($row['total']);
            }

            return $data;
        }
    }

    public static function getOrderDetail($order_id)
    {
        $sql = <<<SQL
SELECT d.ddh_id, DATE_FORMAT(d.ddh_ngaydh, '%d/%m/%Y') AS ddh_ngaydh, ddh_diachigh, ddh_sodtnh, ddh_trangthaixn, ddh_trangthaigh,
       dt_ten, ctddh_soluongdh, ctddh_dongiadh
FROM dondathang d NATURAL JOIN chitietddh c NATURAL JOIN dienthoai dt
WHERE d.ddh_id = :id
SQL;
        $items = self::$_database->run($sql, array('id' => $order_id));

        $total = 0;
        foreach ($items as &$item) {
            $item['price'] = App::formatVnd($item['ctddh_dongiadh']);
            $item['name'] =& $item['dt_ten'];
            $item['quantity'] =& $item['ctddh_soluongdh'];
            $item['total'] = App::formatVnd($item['ctddh_dongiadh'] * $item['ctddh_soluongdh']);
            $total += $item['ctddh_dongiadh'] * $item['ctddh_soluongdh'];
        }

        $sql = <<<SQL
SELECT DATE_FORMAT(ddh_ngaydh, '%d/%m/%Y') AS ddh_ngaydh, ddh_diachigh, ddh_sodtnh, ddh_trangthaixn, ddh_trangthaigh,
       u_ten
FROM dondathang NATURAL JOIN mbuser
WHERE ddh_id = :id
SQL;
        $orderInfo = self::$_database->run($sql, array('id' => $order_id));
        if (count($orderInfo) > 0) {
            $orderInfo = $orderInfo[0];
        }


        return array(
            'items' => $items,
            'total' => App::formatVnd($total),
            'order' => $orderInfo
        );

    }

    public static function delete($order_id)
    {
        # Xóa chi tiết đơn hàng
        self::$_database->run('DELETE FROM chitietddh WHERE ddh_id = :id', array('id' => $order_id));

        # XÓa đơn đặt hàng
        self::$_database->run('DELETE FROM dondathang WHERE ddh_id = :id', array('id' => $order_id));
    }

    public static function confirm($order_id)
    {
        self::$_database->run('UPDATE dondathang SET ddh_trangthaixn = 1 WHERE ddh_id = :id', array('id' => $order_id));
    }

    public static function deliver($order_id)
    {
        self::confirm($order_id);
        self::$_database->run('UPDATE dondathang SET ddh_trangthaigh = 1 WHERE ddh_id = :id', array('id' => $order_id));
    }
}